package com.bulkmail.sender.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.bulkmail.sender.DataSession;
import com.bulkmail.sender.MimeApp;
import com.bulkmail.sender.WriteXMLFile;
import com.bulkmail.sender.WriteXMLOptionFile;

public class StartActionListener implements ActionListener {

	private static final String MAIL_SESSION = "src/main/resources/file.xml";
	private static final String MAIL_OPTIONS = "src/main/resources/optional.xml";

	private MainFrame frame;

	public StartActionListener(MainFrame frame) {
		this.frame = frame;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		DataSession dataSession = new DataSession();

		int nrEmails;
		try{
			nrEmails = Integer.parseInt(frame.noOfEmailTextField.getText());
		}catch(NumberFormatException e1){
			JOptionPane.showMessageDialog(frame,
				    "Number of e-mails must be an integer lower than 10000.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
			return;
		}
		if(nrEmails > 10000){
			JOptionPane.showMessageDialog(frame,
				    "Number of e-mails must be an integer lower than 10000",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		dataSession.setNrEmails(Integer.valueOf(frame.noOfEmailTextField
				.getText()));
		
		int seconds;
		try{
			seconds = Integer.parseInt(frame.timeBetweenTextField.getText());
		}catch(NumberFormatException e1){
			JOptionPane.showMessageDialog(frame,
				    "Number of seconds must be an integer lower than 60000.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
			return;
		}
		if(seconds > 60000){
			JOptionPane.showMessageDialog(frame,
				    "Number of seconds must be an integer lower than 60000.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			return;
		}
		dataSession.setSeconds(Integer.valueOf(frame.timeBetweenTextField
				.getText()));
		dataSession.setSmtp(frame.smtpRadioButton.isSelected());
		dataSession.setExs(frame.exchangeRadioButton.isSelected());
		dataSession.setAddCounter(frame.counterCheckBox.isSelected());
		dataSession.setAddRandomSubject(frame.randomCheckBox.isSelected());

		dataSession.setAttachPath(frame.attachmentTextField.getText());
		String server = "";
		
		
			if ((frame.serverTextField.getText()).equals("")){
				JOptionPane.showMessageDialog(frame,
					    "Server field must be filled.",
					    "Validation error",
					    JOptionPane.ERROR_MESSAGE);
				return;
			}
			
			dataSession.setServer(frame.serverTextField.getText());
		
		
		int port;
		try {
			port = Integer.parseInt(frame.portTextField.getText());
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(frame,
				    "Port must be an integer lower than 1024.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
			return;
		}
		if(port > 1024){
			JOptionPane.showMessageDialog(frame,
				    "Port must be an integer lower than 1024.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		
	//	System.out.println(port);
		dataSession.setPort(frame.portTextField.getText());
		
		if ((frame.userTextField.getText()).equals("")){
			JOptionPane.showMessageDialog(frame,
				    "User field must be filled.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			return;
		}
		dataSession.setUsername(frame.userTextField.getText());
		if ((frame.passwordField.getPassword()).length <= 3){
			JOptionPane.showMessageDialog(frame,
				    "Password field must be at least 4 characters long.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		dataSession.setPassword(new String(frame.passwordField.getPassword()));
		if ((frame.fromTextField).getText().equals("")){
			JOptionPane.showMessageDialog(frame,
				    "From field must be filled.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		dataSession.setFrom(frame.fromTextField.getText());
		
		if ((frame.toTextField).getText().equals("")){
			JOptionPane.showMessageDialog(frame,
				    "Send To field must be filled.",
				    "Validation error",
				    JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		List<String> temp = new ArrayList<String>();
		String[] toMany = frame.toTextField.getText().split(";");
		for(int i = 0; i < toMany.length; i++){
			temp.add(toMany[i]);
		}		
		dataSession.setTo(temp);
		dataSession.setSubject(frame.subjectTextField.getText());
		dataSession.setMessage(frame.messageArea.getText());

		WriteXMLFile writeSession = new WriteXMLFile(dataSession, MAIL_SESSION);
		WriteXMLOptionFile writeOptions = new WriteXMLOptionFile(dataSession,
				MAIL_OPTIONS);
		writeSession.writeXML();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		writeOptions.writeXMLOptionFile();

		MimeApp mimeApp = new MimeApp();

		if ((dataSession.getAttachPath()).equals("")) {
			mimeApp.sendMime(dataSession.getAttachPath(), false, dataSession);
		} else {
			mimeApp.sendMime(dataSession.getAttachPath(), true, dataSession);
		}
	}
}
